package com.jms.product.service.impl;

import com.jms.product.constant.RegisterSourceEnum;
import com.jms.product.entity.UserAuthEntity;
import com.jms.product.entity.UserBaseEntity;
import com.jms.product.feign.ThirdPartyFeignService;
import com.jms.product.service.RegisterService;
import com.jms.product.service.UserAuthService;
import com.jms.product.service.UserBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Jamison
 * @version 1.0
 * @date 2021/6/11 14:59
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private ThirdPartyFeignService thirdPartyFeignService;

    @Autowired
    private UserAuthService userAuthService;

    @Autowired
    private UserBaseService userBaseService;

    /**
     * 保存手机号登录的用户认证信息
     * @param mobile 手机号
     * @param entity 用户的基本信息
     */
    @Override
    public void initUserAuth(String mobile, UserBaseEntity entity) {
        // 初始化六位数密码，往UserAuthEntity表中插入数据
        UserAuthEntity userAuthEntity = new UserAuthEntity();
        userAuthEntity.setUid(entity.getUid());
        // 随机生成一个六位数密码
        String password = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        // 加密后的密码，保存到数据库
        String encode = encoder.encode(password);
        userAuthEntity.setCertificate(encode);
        userAuthEntity.setIdentityType(RegisterSourceEnum.PHONE.getType());
        userAuthEntity.setIdentifier(mobile);
        userAuthEntity.setCreateTime(new Date());
        userAuthEntity.setUpdateTime(userAuthEntity.getCreateTime());
        userAuthService.save(userAuthEntity);

        // 发送密码初始化短信通知
        thirdPartyFeignService.sendPasswordSms(mobile, password);
    }

    /**
     * 保存用户的基本信息
     * @param mobile 手机号
     * @return 返回保存用户的基本信息
     */
    @Override
    public UserBaseEntity saveBaseUser(String mobile) {
        // 验证码正确，注册
        UserBaseEntity entity = new UserBaseEntity();
        entity.setMobile(mobile);
        entity.setUserName(mobile);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(entity.getCreateTime());
        entity.setMobileBindTime(entity.getCreateTime());
        // 注册类型是手机号注册
        entity.setRegisterSource(RegisterSourceEnum.PHONE.getType());
        userBaseService.save(entity);
        return entity;
    }

    /**
     * 保存微博登录的认证信息
     * @param register 用户的基本信息
     */
    @Override
    public void initUserAuthWithWeiboLogin(UserBaseEntity register) {
        UserAuthEntity userAuthEntity = new UserAuthEntity();
        userAuthEntity.setUid(register.getUid());
        userAuthEntity.setIdentityType(RegisterSourceEnum.SINA_WEIBO.getType());
        userAuthEntity.setIdentifier(register.getUserName());
        userAuthEntity.setCertificate(register.getAccessToken());
        userAuthEntity.setCreateTime(new Date());
        userAuthEntity.setUpdateTime(userAuthEntity.getCreateTime());
        userAuthService.save(userAuthEntity);
    }

}
